<template>
  <div class="product_con">
    商品列表
        <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="el-icon-search"></span> <span>筛选搜索</span>
        <el-button
          style="float: right; margin-left: 5px"
          type="primary"
          size="small"
          @click="searchFun"
          >查询</el-button
        >
        <el-button style="float: right" type="button" size="small"
        @click="congzhi"
          >重置</el-button
        >
      </div>
        <el-form ref="form" :model="listQuery" label-width="80px">
        <el-form-item label="输入搜索">
          <el-input v-model="listQuery.name" placeholder="商品名称"></el-input>
        </el-form-item>
        <el-form-item label="商品货号">
          <el-input
            v-model="listQuery.product_sn"
            placeholder="商品货号"
          ></el-input>
        </el-form-item>
        <el-form-item label="上架状态">
          <el-select v-model="listQuery.publish_status" placeholder="全部">
            <el-option
              v-for="item in publishStatusOptions"
              :label="item.label"
              :value="item.value"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品品牌">
          <el-select v-model="listQuery.brand_id" placeholder="全部">
            <el-option
              v-for="item in brandOptions"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

         <el-form-item label="商品分类">
          <el-cascader
            v-model="productCateValue"
            :options="productCateOptions"
          ></el-cascader>
        </el-form-item>
      </el-form>
     
         </el-card>

         <el-card class="box-card">
      <div>
        <span class="el-icon-goods"></span>
        <span> 数据列表</span>
        <el-button
          style="float: right"
          type="button"
          size="small"
          @click="goAddPage"
          >添加</el-button
        >
      </div>
    </el-card>
     
      
    <el-table :data="productList" border style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column label="商品图片">
        <template slot-scope="scope">
          <img :src="scope.row.pic" class="product_img" />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="商品名称"> </el-table-column>

      <el-table-column label="价格/货号">
        <template slot-scope="scope">
          <div>价格： {{ scope.row.price }}</div>
          <div>货号： {{ scope.row.product_sn }}</div>
        </template>
      </el-table-column>

      <el-table-column label="标签">
        <template slot-scope="scope">
          <div>上架：{{ scope.row.publish_status | statusType }}</div>
          <div>新品：{{ scope.row.new_status | statusType }}</div>
          <div>推荐：{{ scope.row.recommand_status | statusType }}</div>
        </template>
      </el-table-column>

      <el-table-column label="审核状态">
        <template slot-scope="scope">
          {{ scope.row.verify_status | verifyType }}
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="editFun(scope.row.id)"
            >编辑</el-button
          >
          <el-button type="danger" size="small" @click="delFun(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
     
    </el-table>
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

     
  </div>
</template>

<script>
//import axios from "axios"
import { fetchProductList ,fetchBrandList,fetchCateList,deleteProduct} from "@/api/product";
export default {
  name: "product",
  data: function () {
    return {
      productList: [],
      listQuery:{
          pageSize:10,
          pageNum:1,
          name:"",
          product_sn:null,
          publish_status:null,
          brand_id:null,
            product_category_id: null,

      },
      
     brandOptions:[],
     productCateOptions:[],
     productCateValue:null,
        publishStatusOptions:[
            {value:1,label:"上架"},
            {value:0,label:"下架"}
        ],
        
          total:0,
    };
  },
  mounted() {
    //调用 获取 列表的函数
    this.getProductData();
    this.getBrandData();
    this.getCateData();
  },
 //mounted() {
//     axios
//       .get("http://yinruifang.cn/index/Api/product_list")
//       .then((res) => {
//         this.productList = res.data.product;

//       });
      
 // },

  //过滤器
  filters: {
    verifyType(value) {
      //console.log(value);
      return value ? "已审核" : "未审核";
    },
    statusType(value) {
      //console.log(value);
      return value ? "是" : "否";
    },
  },
  methods: {
    handleSizeChange(val){
        console.log("修改每页显示的条数 触发了", val)
        this.listQuery.pageSize = val;
        this.listQuery.pageNum = 1;
        this.getProductData();
    },
  handleCurrentChange(val){
       console.log("修改了当前页面触发了",val);
       this.listQuery.pageNum = val;
       this.getProductData();
   },
    async getProductData() {
      var res = await fetchProductList(this.listQuery);
      this.productList = res.product;
      this.total = res.total;
    },
    async getBrandData(){
        var res = await fetchBrandList();
        console.log(res);
        this.brandOptions = res.data;
    },
   async getCateData() {
      var res = await fetchCateList();
      console.log(res);
      //重组数据
      var data = res.data;
      data.forEach((i) => {
        let children = []; //放子节点数据
        //重组子级节点数据
        i.children.forEach((j) => {
          children.push({
            label: j.name,
            value: j.id,
          });
        });
        this.productCateOptions.push({
          label: i.name,
          value: i.id,
          children: children,
        });
      });
      console.log(this.productCateOptions);
    },
  //   watch: {
  //   //监听级联菜单的选中的数据是否发生变化了
  //   productCateValue(newV) {
  //     console.log(newV);
  //     //找到 数组的1号元素，赋值给 product_category_id
  //     this.listQuery.product_category_id = newV[1];
  //   },
  // },
     searchFun(){
        this.getProductData();

    },
    congzhi(){
      this.listQuery = {
        
          pageSize:10,
          pageNum:1,
          name:"",
          product_sn:null,
          publish_status:null,
          brand_id:null,
          
      }
    },
     delFun(id){
   console.log("删除触发了", id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用删除接口
          deleteProduct({ id: id }).then((res) => {
            console.log(res);
            if (res.type == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
             this.getProductData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    goAddPage(){
        this.$router.push("/pms/productadd");
    }
},
 
   
//   methods: {
//     //修改
//     editFun(id) {
//       console.log("修改触发了", id);
//     },
//     //删除
//     delFun(id) {
//       console.log("删除触发了", id);
//     },
//   },
};
</script>

<style scoped lang="scss">
.product_con {
  margin: 20px;
  .product_img {
    width: 100px;
  }
}
</style>